<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * CurrentResearch list Model
 */
class NrmResearchModelCurrentResearchs extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
					'id', 'a.id',
					'year_of_publication', 'a.year_of_publication',
					'title', 'a.title',
			);
		}	
		parent::__construct($config);
	}
	protected function getListQuery()
	{
		// Create a new query object.
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		// Select some fields
		$query->select('a.*');
		// From the current project table
		$query->from('#__nrmresearch_publications as a');
		
		if ($this->getState('keywords0')) {
			$where = '(a.title like "%'.$this->getState('keywords0').'%"
				or a.keywords like "%'.$this->getState('keywords0').'%"
				or a.summary like "%'.$this->getState('keywords0').'%"
				or a.phone like "%'.$this->getState('keywords0').'%"
				or a.email like "%'.$this->getState('keywords0').'%"
				or a.lead_organisation like "%'.$this->getState('keywords0').'%"
				or a.partner_organisation_s like "%'.$this->getState('keywords0').'%"
				or a.chief_investigator like "%'.$this->getState('keywords0').'%"
				)';
				$query->where($where);
		}
		
		if ($this->getState('keywords1')) {
			$where = '(a.title like "%'.$this->getState('keywords1').'%"
			or a.keywords like "%'.$this->getState('keywords1').'%"
			or a.summary like "%'.$this->getState('keywords1').'%"
			or a.phone like "%'.$this->getState('keywords1').'%"
			or a.email like "%'.$this->getState('keywords1').'%"
			or a.lead_organisation like "%'.$this->getState('keywords1').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords1').'%"
			or a.chief_investigator like "%'.$this->getState('keywords1').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords2')) {
			$where = '(a.title like "%'.$this->getState('keywords2').'%"
			or a.keywords like "%'.$this->getState('keywords2').'%"
			or a.summary like "%'.$this->getState('keywords2').'%"
			or a.phone like "%'.$this->getState('keywords2').'%"
			or a.email like "%'.$this->getState('keywords2').'%"
			or a.lead_organisation like "%'.$this->getState('keywords2').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords2').'%"
			or a.chief_investigator like "%'.$this->getState('keywords2').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords3')) {
			$where = '(a.title like "%'.$this->getState('keywords3').'%"
			or a.keywords like "%'.$this->getState('keywords3').'%"
			or a.summary like "%'.$this->getState('keywords3').'%"
			or a.phone like "%'.$this->getState('keywords3').'%"
			or a.email like "%'.$this->getState('keywords3').'%"
			or a.lead_organisation like "%'.$this->getState('keywords3').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords3').'%"
			or a.chief_investigator like "%'.$this->getState('keywords3').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords4')) {
			$where = '(a.title like "%'.$this->getState('keywords4').'%"
			or a.keywords like "%'.$this->getState('keywords4').'%"
			or a.summary like "%'.$this->getState('keywords4').'%"
			or a.phone like "%'.$this->getState('keywords4').'%"
			or a.email like "%'.$this->getState('keywords4').'%"
			or a.lead_organisation like "%'.$this->getState('keywords4').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords4').'%"
			or a.chief_investigator like "%'.$this->getState('keywords4').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords5')) {
			$where = '(a.title like "%'.$this->getState('keywords5').'%"
			or a.keywords like "%'.$this->getState('keywords5').'%"
			or a.summary like "%'.$this->getState('keywords5').'%"
			or a.phone like "%'.$this->getState('keywords5').'%"
			or a.email like "%'.$this->getState('keywords5').'%"
			or a.lead_organisation like "%'.$this->getState('keywords5').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords5').'%"
			or a.chief_investigator like "%'.$this->getState('keywords5').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords6')) {
			$where = '(a.title like "%'.$this->getState('keywords6').'%"
			or a.keywords like "%'.$this->getState('keywords6').'%"
			or a.summary like "%'.$this->getState('keywords6').'%"
			or a.phone like "%'.$this->getState('keywords6').'%"
			or a.email like "%'.$this->getState('keywords6').'%"
			or a.lead_organisation like "%'.$this->getState('keywords6').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords6').'%"
			or a.chief_investigator like "%'.$this->getState('keywords6').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords7')) {
			$where = '(a.title like "%'.$this->getState('keywords7').'%"
			or a.keywords like "%'.$this->getState('keywords7').'%"
			or a.summary like "%'.$this->getState('keywords7').'%"
			or a.phone like "%'.$this->getState('keywords7').'%"
			or a.email like "%'.$this->getState('keywords7').'%"
			or a.lead_organisation like "%'.$this->getState('keywords7').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords7').'%"
			or a.chief_investigator like "%'.$this->getState('keywords7').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords8')) {
			$where = '(a.title like "%'.$this->getState('keywords8').'%"
			or a.keywords like "%'.$this->getState('keywords8').'%"
			or a.summary like "%'.$this->getState('keywords8').'%"
			or a.phone like "%'.$this->getState('keywords8').'%"
			or a.email like "%'.$this->getState('keywords8').'%"
			or a.lead_organisation like "%'.$this->getState('keywords8').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords8').'%"
			or a.chief_investigator like "%'.$this->getState('keywords8').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('keywords9')) {
			$where = '(a.title like "%'.$this->getState('keywords9').'%"
			or a.keywords like "%'.$this->getState('keywords9').'%"
			or a.summary like "%'.$this->getState('keywords9').'%"
			or a.phone like "%'.$this->getState('keywords9').'%"
			or a.email like "%'.$this->getState('keywords9').'%"
			or a.lead_organisation like "%'.$this->getState('keywords9').'%"
			or a.partner_organisation_s like "%'.$this->getState('keywords9').'%"
			or a.chief_investigator like "%'.$this->getState('keywords9').'%"
			)';
			$query->where($where);
		}
		
		if ($this->getState('research_location')) {
			$query->where('a.research_location like "%'.$this->getState('research_location').'%"');
		}
		
		if ($this->getState('investigator')) {
			$query->where('a.chief_investigator like "%'.$this->getState('investigator').'%"');
		}
				
		if ($this->getState('organisation_involved')) {
			$query->where('(a.partner_organisation_s like "%'.$this->getState('organisation_involved').'%" or a.lead_organisation like "%'.$this->getState('organisation_involved').'%" )');
		}
		
		if ($this->getState('type_of_project')) {
			$query->where('a.type_of_project like "%'.$this->getState('type_of_project').'%"');
		}
		$query->where('CONVERT( substring( `period_of_funding` , 6 ) , UNSIGNED INTEGER ) >= YEAR( NOW( ))');
		//Add the list ordering clause.
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.id')).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));
		//echo "start = ". $this->getState("list.start") . "    and limit = " . $this->getState("list.limit") ;		
		//echo $query;//die;
		//var_dump(JRequest::get());		
		return $query;
	}	
	protected function populateState($ordering = null, $direction = null)
	{	
		// Initialise variables.
		$app	= JFactory::getApplication();
		
		// Load the parameters.
		$params = $app->getParams();
		$this->setState('params', $params);
		
		$db		= $this->getDbo();
		
		$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', MAX_NUMS_ITEMS_PER_PAGE);		
		$this->setState('list.limit', $limit);
		$perpage = JRequest::getVar('limit', 0, '', 'int');		
		if($perpage != 0 )$this->setState('list.limit', $perpage);
		
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$this->setState('list.start', $limitstart);
		
		$orderCol	= JRequest::getVar('filter_order', 'id');		
		if (!in_array($orderCol, $this->filter_fields)) {			
			$orderCol = 'id';
		}
		$this->setState('list.ordering', $orderCol);
		
		$listOrder	=  JRequest::getCmd('filter_order_Dir', 'DESC');
		if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
			$listOrder = 'ASC';
		}
		$this->setState('list.direction', $listOrder);
		
		$keywords0 = JRequest::getVar('keywords0','','','string');
		if($keywords0==KEYWORD_LABEL)$keywords0 = '';
		$this->setState('keywords0',$keywords0);
		
		$keywords1 = JRequest::getVar('keywords1','','','string');
		if($keywords1==KEYWORD_LABEL)$keywords1 = '';
		$this->setState('keywords1',$keywords1);
		
		$keywords2 = JRequest::getVar('keywords2','','','string');
		if($keywords2==KEYWORD_LABEL)$keywords2 = '';
		$this->setState('keywords2',$keywords2);
		
		$keywords3 = JRequest::getVar('keywords3','','','string');
		if($keywords3==KEYWORD_LABEL)$keywords3 = '';
		$this->setState('keywords3',$keywords3);
		
		$keywords4 = JRequest::getVar('keywords4','','','string');
		if($keywords4==KEYWORD_LABEL)$keywords4 = '';
		$this->setState('keywords4',$keywords4);
		
		$research_location = JRequest::getVar('research_location','','','string');		
		if($research_location == RES_LOCATION)$research_location = '';
		$this->setState('research_location',$research_location);
		
		$investigator = JRequest::getVar('investigator','','','string');
		if($investigator == INVESTIGATOR_LABEL)$investigator = '';
		$this->setState('investigator',$investigator);
		
		$organisation_involved = JRequest::getVar('organisation_involved','','','string');
		if($organisation_involved == ORGANISATION_INVOLVED_LABEL)$organisation_involved = '';
		$this->setState('organisation_involved',$organisation_involved);
		
		$type_of_project = JRequest::getVar('type_of_project','','','string');
		if($type_of_project == TYPE_OF_PROJECT_LABEL)$type_of_project = '';
		$this->setState('type_of_project',$type_of_project);		
	}	
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
			require_once(JPATH_COMPONENT.DS.'libs'.DS.'pagination.php');
			$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
			$limit = $this->getState('list.limit',MAX_NUMS_ITEMS_PER_PAGE);
			$this->_pagination = new JPagination4Grid($this->getTotal(), $limitstart, $limit );
		}
		return $this->_pagination;
	}
}
